package com.fs.controller;


import com.fs.entity.Materialcategory;
import com.fs.entity.Materials;
import com.fs.entity.ResponseData;
import com.fs.entity.Students;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.fs.service.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-06-12
 */
@RestController
@RequestMapping("/students")
public class StudentsController {
    @Autowired
    IMaterialcategoryService iMaterialcategoryService;
    @Autowired
    IMaterialsService MaterialsServiceImpl;
    @Autowired
    IStudentsService studentsService;
    //获取材料类别名称1111
    @GetMapping("/GetKind")
    public ResponseData getKind(int mckind){
        List<Materialcategory> categories = iMaterialcategoryService.getKind(mckind);
        return ResponseData.ok(categories);

    }
    @PostMapping("/login")
    public ResponseData login(int sid, String password) {
        studentsService.login(sid, password);
        return ResponseData.ok();
    }
    @PostMapping("/handleMaterial")
    public ResponseData handleMaterial(Materials material){
        //todo 完善提交流程
        boolean result = MaterialsServiceImpl.handle(material);
        if (result){
            return ResponseData.ok();
        }
        return ResponseData.error("提交失败");
    }
    @GetMapping("/logout")
    //用作退出登录
    public ResponseData logout() {
        //让session失效
        StudentsContext.getSession().invalidate();
        return ResponseData.ok();
    }

    @PostMapping("/register")
    public ResponseData register( Students students) {
        if (studentsService.register(students)) {
            return ResponseData.ok(200, "注册成功");
        } else {
            return ResponseData.error(500, "注册失败");
        }
    }

    //修改个人信息
    @PostMapping("/modify")
    public ResponseData modify(Students students){
        if(studentsService.modify(students)){
            return ResponseData.ok(200,"修改成功");
        }
        else {
            return ResponseData.error(500,"修改失败");
        }
    }
    //获取材料详情
    @GetMapping("getMaterialDetail")
    public ResponseData getMaterialDetail(int mid){
        return ResponseData.ok(MaterialsServiceImpl.getMaterialDetail(mid));
    }
    @DeleteMapping("withdrawMaterial")
    public ResponseData withdrawMaterial(Integer mid){
        boolean result = MaterialsServiceImpl.withdrawMaterial(mid);
        if (result){
            return ResponseData.ok("撤回成功");
        }
        return ResponseData.error("撤回失败");
    }
    
    //查询已获得的学分值
    @GetMapping("studentid")
    public ResponseData queryBysid(Integer sid){
       int crd=studentsService.queryBySid(sid);
        return ResponseData.ok(200);
    }
    //todo 修改材料
}
